import request from '@/utils/request';
import {getCookie} from "@/utils/cookie";

export function logIn(form) {
    const id = Number(form.id);
    if (isNaN(id)) {
        return new Promise((resolve, reject) => {
            reject('ID is not number');
        });
    }
    const { password } = form;
    const csrfToken = getCookie('csrfToken');
    return request({
        method: 'post',
        url: '/login',
        data: {
            id,
            password,
        },
        headers: {
            'x-csrf-token': csrfToken,
        },
    });
}

export function signUp(form) {
    const { name, password } = form;
    if (name.length > 29) {
        return new Promise((resolve, reject) => {
            reject('Length of name exceeds 29');
        });
    }
    const csrfToken = getCookie('csrfToken');
    return request({
        method: 'post',
        url: '/signup',
        data: {
            name,
            password,
        },
        headers: {
            'x-csrf-token': csrfToken,
        },
    });
}
